<html>
<head>
	<script type="text/javascript" src="../../lib/contrib/jsbn/jsbn.js"></script>
	<script type="text/javascript" src="../../lib/contrib/jsbn/jsbn2.js"></script>
<script type="text/javascript" src="../../lib/big-number.js"></script>
<script type="text/javascript">

	function find_form_field(k) {
		return document.getElementById(k);
	}

	function get_form_value(k) {
		var f = find_form_field(k);
		if (null == f) { return; }
		return f.value;
	}

	function set_form_value(k, v) {
		var f = find_form_field(k);
		if (null == f) { return; }
		f.value = v;
	}

	function format_digits(a) {
		var o = "";
		var nine = new BigInteger("9", 10);
		for (var i in a) {
			if (a[i].compareTo(nine) > 0) {
				o += "[" + a[i] + "]";
			} else {
				o += a[i];
			}
		}
		return o;
	}

	function clicked_fraction_decimal() {
		var n = new_big_int(get_form_value("numerator"), 10);
		var d = new_big_int(get_form_value("denominator"), 10);
		var b = new_big_int(get_form_value("base"), 10);
		var a = big_fraction_to_repeating(
				{numerator: n, denominator: d, base: b});
		//alert(n + "/" + d + " -> \"decimal\"");
		set_form_value("before", format_digits(a.before));
		set_form_value("after", format_digits(a.after));
		set_form_value("repeating", format_digits(a.repeating));
	}

	function clicked_decimal_fraction() {
		alert("\"decimal\" -> fraction");
	}

</script>
</head>
<body>
<form action="#" />
	<table>
		<tr>
			<td colspan="3" align="right">fraction is in <strong>base 10</strong></td>
			<td>&nbsp;</td>
			<td colspan="4">"decimal" is in base
				<input id="base" value="10" />
			</td>
		<tr>
			<td>numerator</td>
			<td>&nbsp;</td>
			<td>denominator</td>
			<td>&nbsp;</td>
			<td>whole</td>
			<td>&nbsp;</td>
			<td>non-repeating</td>
			<td>repeating</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
		<td><input id="numerator" /></td>
		<td>/</td>
		<td><input id="denominator" /></td>
		<td>=</td>
		<td><input id="before" /></td>
		<td>.</td>
		<td><input id="after" /></td>
		<td><input id="repeating" /></td>
		<td>...</td>
		</tr>
		<tr>
			<td colspan="3" align="right" /><input 
				type="button" 
				value="fraction -&gt; &quot;decimal&quot;" 
				onclick="clicked_fraction_decimal()"
			/></td>
			<td>&nbsp;</td>
			<td colspan="4" align="left" /><input 
				type="button" 
				value="&quot;decimal&quot; -&gt; fraction" 
				onclick="clicked_decimal_fraction()"
			/></td>
		</tr>
</form>
</body>
</html>
